<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use think\Db;
use cmf\controller\AdminBaseController;
use app\admin\model\cookbookModel;

class CookbookController extends AdminBaseController
{
    /**
     * 菜谱列表
     * @adminMenu(
     *     'name'   => '菜谱列表',
     *     'parent' => '',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '菜谱列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $result  = Db::name('cookbook')->paginate(20);
        
        $this->assign('list', $result->items());
        $this->assign('page', $result->render());
        return $this->fetch();
    }

    /**
     * 菜谱添加
     * @adminMenu(
     *     'name'   => '菜谱添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '菜谱添加',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 菜谱添加提交
     * @adminMenu(
     *     'name'   => '菜谱添加提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '菜谱添加提交',
     *     'param'  => ''
     * )
     */
    public function addPost(){
        $data = $this->request->param();
        Db::name('cookbook')->insert($data);
        $this->success("添加成功！",url('index'));
    }

    /**
     * 菜谱编辑
     * @adminMenu(
     *     'name'   => '菜谱编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '菜谱编辑',
     *     'param'  => ''
     * )
     */
    public function edit(){
        $id     = $this->request->param('id');
        $result = Db::name('cookbook')->where(['id' => $id])->find();
        $this->assign('info', $result);
        return $this->fetch();
    }

    /**
     * 菜谱编辑
     * @adminMenu(
     *     'name'   => '菜谱编辑提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '菜谱编辑提交',
     *     'param'  => ''
     * )
     */
    public function editPost(){
        $data = $this->request->param();
        Db::name('cookbook')->update($data);

        $this->success("保存成功！",url('index'));

    }

    /**
     * 菜谱删除
     * @adminMenu(
     *     'name'   => '菜谱删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '菜谱删除',
     *     'param'  => ''
     * )
     */
    public function delete(){
        $id     = $this->request->param('id', 0, 'intval');
        $result = Db::name('cookbook')->delete($id);
        if ($result) {
            $this->success("删除成功！");
        } else {
            $this->error('删除失败！');
        }

    }

    /**
     * 菜谱隐藏
     * @adminMenu(
     *     'name'   => '菜谱隐藏',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '菜谱隐藏',
     *     'param'  => ''
     * )
     */
    public function ban(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
            $rst = Db::name('cookbook')->where(['id' => $id])->update(['status' => 0]);
            if ($rst) {
                $this->success("菜谱隐藏成功！");
            } else {
                $this->error('菜谱隐藏失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    /**
     * 菜谱显示
     * @adminMenu(
     *     'name'   => '菜谱显示',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '菜谱显示',
     *     'param'  => ''
     * )
     */
    public function cancelBan(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id) {
            $result = Db::name('cookbook')->where(['id' => $id])->update(['status' => 1]);
            if ($result) {
                $this->success("菜谱启用成功！");
            } else {
                $this->error('菜谱启用失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

}